<template>
	<view class="content bgGray">
		<!-- 未登录 -->
		<template v-if="!isLogin">
			<view
				class="textCenter"
				style="width: 90%; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%)"
			>
				<view>为了提供更优质的服务，请先登录</view>
				<view class="l-btn mt16 textCenter white" @tap="handleTap('/pages_B/login/login')">去登录</view>
			</view>
		</template>
		<!-- 已登录 -->
		<template v-else>
			<view class="head p16 flex-r ver-c">
				<image
					src="https://www.helloimg.com/i/2024/09/06/66dab903bae29.png"
					class="author"
					mode="aspectFill"
				></image>
				<view class="f1 pl16" v-if="loginState == 1">
					<view class="white fs13">{{ info.userName }}</view>
					<view class="white mt6 fs13">用户级别：普通用户</view>
				</view>
				<view class="f1 pl16" v-else>
					<view class="white fs13">{{ shopInfo.userName }}</view>
					<view class="white mt6 fs13">用户级别：商户</view>
					<view class="white mt6 fs13">
						商户状态：
						<text>
							{{
								shopInfo.tExaminestatus == 1
									? '未审核'
									: shopInfo.tExaminestatus == 2
									? '已通过'
									: '已驳回'
							}}
						</text>
					</view>
					<view class="white mt6 fs13">店铺浏览量：{{ shopInfo.tPageview }}</view>
				</view>
			</view>
			<!-- 商户的我的 -->
			<template v-if="loginState == 2">
				<view class="m10 fs13 gray textCenter">首次注册，请先在商户信息中上传营业执照审核资质！</view>
				<view class="bgWhite m10 pb10" style="border-radius: 10rpx">
					<view
						class="flex-r jsb"
						v-for="(item, index) of navlist"
						:key="index"
						style="padding: 20rpx 20rpx 20rpx 20rpx; border-bottom: 1rpx solid #eee"
						@tap="handleTap(item.path, item.name)"
					>
						<view class="f1 flex-r ver-c">
							<image :src="item.ico" style="width: 60rpx; height: 60rpx" mode="aspectFill"></image>
							<view class="ml10">{{ item.name }}</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="red mt16 mb16 textCenter" v-if="shopInfo.tExaminestatus == 2">
					我的邀请码：{{ shopInfo.tInvitationcode }}
				</view>
			</template>
			<!-- 普通用户的我的 -->
			<template v-else>
				<view class="bgWhite m10" style="border-radius: 10rpx">
					<!-- <view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="handleScan()"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">扫码进店</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view> -->
					<view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="handleLuck()"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">扫码抽奖</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
					<view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="luckList()"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">我的抽奖记录</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
					<!-- <view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="searchList()"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">我的足迹</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view> -->
					<view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="resetUserPwd()"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">修改密码</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
					<view
						class="flex-r jsb"
						style="padding: 20rpx 20rpx 20rpx 32rpx; border-bottom: 1rpx solid #eee"
						@tap="handleTap('', '退出登录')"
					>
						<view class="f1 flex-r ver-c">
							<image
								src="https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png"
								style="width: 40rpx; height: 40rpx"
								mode="aspectFill"
							></image>
							<view class="ml10">退出登录</view>
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</template>
		</template>
		<u-popup :show="showQrcode" mode="center" :round="10" closeable="true" @close="showQrcode = false">
			<div class="textCenter bgWhite p16" style="padding-top: 100rpx" v-if="loginState == 2">
				<uqrcode ref="uqrcode" canvas-id="qrcode" :value="qrText" :start="false" :options="options"></uqrcode>
				<u-button type="info" customStyle="margin:32rpx 0" text="保存到相册" @click="saveQrcode"></u-button>
			</div>
		</u-popup>
	</view>
</template>

<script>
import Utils from '@/utils/utils.js'
import { getUserInfo, getShopInfo, getPrizeRule } from '@/api/mine.js'
export default {
	data() {
		return {
			isLogin: false,
			showQrcode: false,
			options: {
				margin: 10
			},
			info: {
				userName: '',
				tOpenid: '',
				tMemberid: '' //用户id
			},
			qrText: '',
			loginState: null,
			shState: null,
			shopInfo: {
				userName: '',
				tMerchantid: '', // 商户id
				tInvitationcode: '',
				tExaminestatus: null, // 商户状态 1未审核 2审核通过 3审核驳回
				tPageview: '' //浏览量
			},

			navlist: [
				{
					ico: 'https://www.helloimg.com/i/2024/09/06/66dabcd38bdcc.png',
					name: '商户信息',
					path: '/pages_D/merchantReview/merchantReview'
				},
				{
					ico: 'https://www.helloimg.com/i/2024/09/06/66dabcd411239.png',
					name: '我的商品',
					path: '/pages_D/goodsControl/myGoods'
				},
				{
					ico: 'https://www.helloimg.com/i/2024/09/06/66dabcd39cbb1.png',
					name: '新增商品',
					path: '/pages_D/goodsControl/goodsControl'
				},
				{
					ico: 'https://www.helloimg.com/i/2025/03/27/67e5172616cf1.png',
					name: '抽奖奖品设置',
					path: '/pages_D/lucky/luckySetting'
				},
				{
					ico: 'https://www.helloimg.com/i/2025/03/27/67e516d620ac8.png',
					name: '抽奖二维码',
					path: 'qrcode'
				},
				{
					ico: 'https://www.helloimg.com/i/2025/03/28/67e5f399a6389.png',
					name: '抽奖记录',
					path: '/pages_D/lucky/luckyRecord'
				},
				{
					ico: 'https://www.helloimg.com/i/2025/05/19/682ad9025c27b.png',
					name: '我的访客',
					path: '/pages_C/vistorList/vistorList'
				},
				{
					ico: 'https://www.helloimg.com/i/2025/03/27/67e519cf4894d.png',
					name: '修改密码',
					path: '/pages_B/resetPwd/resetPwd'
				},
				{
					ico: 'https://www.helloimg.com/i/2024/09/06/66dabcd415bd0.png',
					name: '退出登录',
					path: ''
				}
			]
		}
	},
	onShow() {
		let state = uni.getStorageSync('loginState') //1 用户 2 商户
		this.loginState = state
		console.log('??', state)
		switch (state) {
			case 1:
				this.getUserInfo()
				this.isLogin = true
				break
			case 2:
				this.getShopInfo()
				this.isLogin = true
		}
	},
	methods: {
		//获取用户信息
		getUserInfo() {
			if (Utils.getUserInfo().tOpenid !== '') {
				this.info.userName = Utils.getUserInfo().userName
				this.info.tOpenid = Utils.getUserInfo().tOpenid
				this.info.tMemberid = Utils.getUserInfo().tMemberid
				return
			}
			getUserInfo().then(
				(res) => {
					if (res.code == 200) {
						this.info.userName = res.tMemberinfo.userName
						this.info.tOpenid = res.tMemberinfo.tOpenid
						Utils.saveUserInfo(res.tMemberinfo.userName, res.tMemberinfo.tOpenid, res.tMemberinfo.tMemberid)
					}
				},
				(err) => {
					console.log(err)
				}
			)
		},
		//获取商户信息
		getShopInfo() {
			getShopInfo().then((res) => {
				console.log(res)
				if (res.code == 200) {
					this.shopInfo.userName = res.tMerchantinfo.userName
					this.shopInfo.tExaminestatus = res.tMerchantinfo.tExaminestatus
					this.shopInfo.tMerchantid = res.tMerchantinfo.tMerchantid
					this.shopInfo.tInvitationcode = res.tMerchantinfo.tInvitationcode
					this.shopInfo.tPageview = res.tMerchantinfo.tPageview
					this.qrText = `https://wx.sdsjms.com?tMerchantid=${res.tMerchantinfo.tMerchantid}`
				}
			})
		},

		handleTap(url, name) {
			// 处理退出登录
			if (name === '退出登录') {
				Utils.showModel('你确定要退出吗').then(
					() => {
						//Utils.clearCaCheA()
						uni.removeStorageSync('tokenKey', '')
						uni.removeStorageSync('loginState', '')
						uni.reLaunch({
							url: '/pages_B/login/login'
						})
					},
					(err) => {
						console.log(err)
						console.log('取消')
					}
				)
				return
			}
			if (name == '抽奖二维码') {
				let tMerchantid = this.shopInfo.tMerchantid
				getPrizeRule(tMerchantid).then((res) => {
					console.log(res)
					if (res.code == 200) {
						if (res.ruleList.length == 0) {
							return Utils.toast('请先设置抽奖奖品')
						} else {
							//console.log('二维码')
							//console.log(this.qrText)
							this.showQrcode = true
							this.$nextTick(() => {
								this.$refs.uqrcode.make({
									success: () => {
										console.log('生成成功')
									},
									fail: (err) => {
										console.log(err)
									}
								})
							})
						}
					} else {
						Utils.toast(res.msg)
					}
				})
				return
			}
			uni.navigateTo({
				url: `${url}?tMerchantid=${this.shopInfo.tMerchantid}`
			})
			console.log(url, name)
		},
		saveQrcode() {
			this.$refs.uqrcode.save({
				success: () => {
					Utils.toast('保存成功')
				}
			})
		},
		// handleScan() {
		// 	uni.scanCode({
		// 		onlyFromCamera: true,
		// 		scanType: ['qrCode'],
		// 		success: function (res) {
		// 			console.log('条码内容：' + res.result)
		// 			let params = res.result
		// 			uni.navigateTo({
		// 				url: `/pages_C/goodList/scanGoodList?${params}`
		// 			})
		// 		}
		// 	})
		// },
		handleLuck() {
			uni.scanCode({
				onlyFromCamera: true,
				scanType: ['qrCode'],
				success: function (res) {
					//console.log('条码内容：' + res.result)
					let params = res.result
					let ids = params.split('=')[1]
					console.log(ids)
					getPrizeRule(ids).then((res) => {
						console.log(res)
						if (res.code == 200) {
							if (res.ruleList.length == 0) {
								return Utils.toast('商家未设置抽奖选项')
							} else {
								let luckName = res.ruleList.map((rule) => rule.tPrizename)
								console.log(luckName)
								uni.navigateTo({
									url: `/pages_D/lucky/luckyDraw?data=${JSON.stringify(luckName)}&tMerchantid=${ids}`
								})
							}
						} else {
							Utils.toast(res.msg)
						}
					})
				}
			})
		},
		luckList() {
			uni.navigateTo({
				url: `/pages_D/lucky/luckyList`
			})
		},
		searchList() {
			uni.navigateTo({
				url: `/pages_C/historyList/historyList`
			})
		},
		resetUserPwd() {
			uni.navigateTo({
				url: `/pages_B/resetPwd/resetPwd`
			})
		}
	}
}
</script>

<style lang="stylus" scoped>
.l-btn
	background linear-gradient(90deg, #f35447 0, #ff8e3c)
	border-radius 50rpx
	line-height 90rpx
.fs16
	font-size 32rpx
	color #282828
.content
	min-height 100%
.head
	background #c40414
	.author
		width 108rpx
		height 108rpx
		border 4rpx solid #fff
		border-radius 100%
.bgWhite > .flex-r:last-child
	border none !important
</style>
